import axios from "axios";
import qs from "qs";
import app from "../main.js";
import {serviceIp} from "../../config/api"

const service = axios.create({
    baseURL: serviceIp,
    timeout: 5000
});


service.interceptors.request.use(config => {
        console.log(config.method)
        config.method === 'post'
            ? config.data = qs.stringify({...config.data})
            : config.params = {...config.params};
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        return config
    },
    error => {
        app.$vux.toast.show({
            type: 'warn',
            text: error
        });
        Promise.reject(error)
    }
);

service.interceptors.response.use(
    response => {
        return response
    },
    error => {
        console.log(error);
        console.log(JSON.stringify(error));

        let text = '请求出错，请重试';
        app.$vux.toast.show({
            type: 'warn',
            text: text
        });
        return Promise.reject(error)
    }
);

export default service
